package bidserver.src;

import java.text.*;
import java.util.*;

//Item store doesn't embed any lock for local access performance.
public class ItemStore {
    SortedMap<Integer, BidItem> items = new TreeMap<Integer, BidItem>(); //map is ordered by the key.

    Set<Integer> getItemIds(String filter) {// filter is for future use.
	return items.keySet();
    }

    public int addItem(String itemTitle, String itemDesc, Date auctionEndTime){//InfoWrite lock? solve it later.
	int itemId = items.isEmpty() ? 1 : items.lastKey()  + 1;
	items.put(itemId, new BidItem(itemId, itemTitle, itemDesc, auctionEndTime));
	return itemId;
    }
    public boolean setItem(int itemId, String itemTitle, String itemDesc, Date auctionEndTime){//InfoWrite Lock, solve later.
	BidItem bidItem = items.get(itemId);
	if( bidItem == null)
	    return false;
	bidItem.item.auctionEndTime = auctionEndTime;
	bidItem.item.itemTitle = itemTitle;
	bidItem.item.itemDesc = itemDesc;
	return true;
    }
    
    ItemInfo getItem(int itemId) {
	BidItem bidItem = items.get(itemId);
	if (bidItem == null)
	    return null;
	return bidItem.item;
    }

    Bid getTopBid(int itemId) {// require Read lock
	BidItem bidItem = items.get(itemId);
	if (bidItem == null || bidItem.bids.isEmpty())
	    return null;
	return bidItem.bids.getLast();
    }

    LinkedList<Bid> getBids(int itemId) {// require readOnly Lock.
	BidItem bidItem = items.get(itemId);
	if (bidItem == null )
	    return null;
	return bidItem.bids;
    }

    Bid PlaceBid(int itemId, int bid, String bidder, Date bidTime) {// require readWrite lock.
						 		// return highest bid.
	// make sure this is the top bid.
	BidItem bidItem = items.get(itemId);
	if (bidItem == null)
	    return null;
	//if time expires.
	if( bidTime.after(bidItem.item.auctionEndTime) )
	    return null;
	if (bidItem.bids.isEmpty() || bid > bidItem.bids.getLast().bid) {
	    // add new bid.
	    bidItem.bids.addLast(new Bid(bid, bidder, bidTime));
	}
	return bidItem.bids.getLast();
    }

    public ItemStore(){
	try {
	    	Date dt = Util.dateFormat.parse("5/1/2011 23:59:59");
	    	String s = Util.dateFormat.format(dt);
	    	Calendar cal = Calendar.getInstance();
	    	
	    	int honda = addItem("Honda Civic", 	"2003 Red", 	Util.dateFormat.parse("6/1/2011 23:59:59"));
	    	int ford = addItem("Ford ", 		"2004 Green", 	Util.dateFormat.parse("6/2/2011 23:59:59"));
    		int gm =   addItem("GM", 		"2005 Blue", 		Util.dateFormat.parse("6/3/2011 23:59:59"));
    		int lexus = addItem("Lexus", 		"2006 Black", 	Util.dateFormat.parse("6/4/2011 23:59:59"));
    		int toyota = addItem("Toyota ", 	"2007 Write", 	Util.dateFormat.parse("6/5/2011 23:59:59"));
    		int jetta = addItem("Jetta", 		"2008 Navy", 	Util.dateFormat.parse("6/6/2011 23:59:59"));
    		int jeep = addItem("Jeep", 		"2009 Metalic", 	Util.dateFormat.parse("6/7/2011 23:59:59"));
    		int benz = addItem("Benz", 		"2010 Gray", 		Util.dateFormat.parse("6/8/2011 23:59:59"));
    		int bmw = addItem("BMW", 		"2011 Blue", 		Util.dateFormat.parse("6/9/2011 23:59:59"));
    		int chevy = addItem("Chevy", 		"2012 Blue", 	Util.dateFormat.parse("6/10/2011 23:59:59"));
    		
    		Bid topBid;
    		topBid = PlaceBid(honda,1000000, "jason", 	cal.getTime());
    		topBid = PlaceBid(honda,1100000, "sashmit",  cal.getTime());
    		topBid = PlaceBid(honda,1200000, "jason", 	cal.getTime());
    		topBid = PlaceBid(honda,1300000, "jason", 	cal.getTime());
    		topBid = PlaceBid(honda,1300000, "jason", 	cal.getTime());
    		topBid = PlaceBid(honda,1300000, "Mike", 	cal.getTime());
    		topBid = PlaceBid(honda,1200000, "jason", 	cal.getTime());
    		topBid = PlaceBid(honda,1000000, "mike", 	cal.getTime());
    		topBid = PlaceBid(lexus,2000000, "jason", 	cal.getTime());
    		topBid = PlaceBid(bmw,  3000000, "mike", 	cal.getTime());
    		topBid = PlaceBid(benz, 4000000, "sashmit",  cal.getTime());
    		topBid = PlaceBid(honda,2000000, "jason", 	cal.getTime());
	} catch (ParseException e) {
	    e.printStackTrace();	       
	}	
    }
    public static void main(String[] args) {
	ItemStore itemStore = new ItemStore();
	Set<Integer> itemIds = itemStore.getItemIds("");
	for (int i : itemIds) {
	    ItemInfo itemInfo = itemStore.getItem(i);
	    Util.print(itemInfo.itemId + " " + itemInfo.itemTitle + " "
		    + itemInfo.itemDesc + " "
		    + Util.dateFormat.format(itemInfo.auctionEndTime));
	    Bid bid = itemStore.getTopBid(i);
	    if (bid != null)
		Util.print(" top bid: " + bid.bid + " " + bid.bidder + " "
			+ Util.dateFormat.format(bid.bidTime));
	    Util.println("");
	    LinkedList<Bid> bids = itemStore.getBids(i);
	    if (bids == null)
		continue;
	    for (Bid b : bids)
		Util.println("\tbid: " + b.bid + " " + b.bidder + " " + Util.dateFormat.format(b.bidTime));
	}    		
    }
}
